package com.hks360.library.util;

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class DateUtil {

	/** 毫秒值 转成年月日 时分秒 */
    public static String parseDate2Str(long d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(d));
    }

    /** 字符串日期转成秒值 */
    public static long parseDate2Second(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(date);
            return d.getTime() / 1000L;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /** 毫秒值 转成年月日 */
    public static String parseDate2StrDay(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(time));
    }
    
    /** 当前系统时间转成年月日 */
    public static String parseDateCurrentTime() {
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         return sdf.format(new Date());
    }
    
    /** 当前系统时间转成年月日时分 */
    public static String parseDateCurrentTime1() {
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
         return sdf.format(new Date());
    }
    
    /** 时间戳转成年月日 */
    public static String parseSecond2Str(long time) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(new Date(time * 1000L));
    }
    
    /** 时间戳转成年月日 */
    public static String parseSecond2Str(String time) {
    	long timeMillus = Long.parseLong(time);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(new Date(timeMillus * 1000L));
    }
    
    /** 毫秒值转成时分秒 */
    public static String parseTime2Str(long time) {
    	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    	return sdf.format(new Date(time));
    }

    /** 获取本月第一天 */
    public static String parseDateMonthFirstDay() {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-01");
        return sdf.format(new Date());
    }
    
    /** 日期解析成毫秒值 */
    public static long parseStrToDateTime(String dateStr,String format) {
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	try {
			return sdf.parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0L;
		}
    }
   
    /** 根据年月获取当月的最大的一天 */
    public static int getMaxDay(int year, int month) {
		int day = 30;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			day = flag ? 29 : 28;
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}
}
